package yxy.game.pm2.active.command.flop;

import yxy.apple.logger.Logger;
import yxy.cherry.data.bean.actfloprandom;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_翻牌;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.Map;

/**
 * 翻牌 - 抽奖奖池
 * @author Dell
 *
 */
@MessageType(proto = Command.ActFlopCardJackpot, classtype = ActiveMessage.ActFlopCardJackpotRequest.class)
public class CMD_Active_翻牌_抽奖_Jackpot extends Command<ActiveMessage.ActFlopCardJackpotRequest> {

	private static final Logger logger = Logger.create(CMD_Active_翻牌_抽奖_Jackpot.class);
	
	@Override
	public void execute(MessageSource messageSource, ActiveMessage.ActFlopCardJackpotRequest request) throws Exception {
		String playercode = messageSource.getTag();
		ActiveMessage.ActFlopCardJackpotResponse.Builder response = ActiveMessage.ActFlopCardJackpotResponse.newBuilder();
		
		ActiveModule_翻牌 am = ActiveModule_翻牌.get(playercode,false);
		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}

		if(!am.running()) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}
		
		Map<String, actfloprandom> afrmap = ActDataCenter.getAllData(actfloprandom.class, playercode);
		for(actfloprandom data : afrmap.values()) {
			if(data.getType() == 2) {
				continue;
			}
			ActiveMessage.FlopCardJackpot.Builder build = ActiveMessage.FlopCardJackpot.newBuilder();
			build.setId(data.getId());
			
			Currency cur = Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum());
			PackageMessage.Item item = new CurrencyHandle().packItem(cur);
			build.setDisplay(item);
			
			response.addJackpot(build.build());
		}
		
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, ActiveMessage.ActFlopCardJackpotResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}
